﻿#ifndef SERVER_H
#define SERVER_H

#include <QObject>
#include <QTcpServer> //监听套接字
#include <QTcpSocket> //通信套接字
#include <QDebug>
#include <QHostAddress>
#include <QProcess>
#include "tankdata.h"
#include <QMetaType>
#include <QDataStream>
class tcpclient : public QObject
{
    Q_OBJECT
public:
    explicit tcpclient(QObject *parent = nullptr);
    ~tcpclient();

    QTcpSocket *tcpSocket = nullptr; // 通信套接字
    QTcpServer *tcpServer = nullptr; // 监听套接字

    bool conectflag = false;

public slots:
    void creattcpclient();
    void write(const char *data, qint64 len);

public:
    void read();
    void read_data(); // 网口数据读取函数

public slots:
    void connectnet(QString IP, quint16 PORT); // 连接到网口
    void disconnectnet();                      // 断开网口
    void sendData(const QString &data);
signals:

    void showstatus(QString);
    void recivedone(QString, Qt::GlobalColor);
    void getheigth(int value, Qt::GlobalColor);
    void displaySASTankStatus();
    void displaySonarWorkingStatus();
    void displayPressureHeight();
    void displayAltitudeSensor();

signals:
    void updateAltitudeDisplay(int altitude);
    void updateUTCDisplay(long  long utc);
    void updateStateDisplay(int state);
    void updateStateDisplay_Mode(int state);
    void canrun();
    void card_state(int);
    void connect_done();    // 连接完成信号
    void disconnect_done(); // 断开完成信号

public:
    bool HexSendTcpClient = true; // 16进制发送
    bool AsciiTcpClient;          // ASCII模式
private:
    QByteArray m_buffer; // 接收数据缓冲区
    pack_Control_Head *m_pack_Control_Head;
    pack_Control_To_PC_SystemTime *systime_Data;

    pack_PC_To_Control_TVG_Gain *Gain_Data;
    pack_PC_To_Control_WorkPower *WorkPower_Data;

    pack_PC_To_Control_WorkMode *WorkMode_Data;
    pack_Control_To_PC_TEMP *TEMP_Data;
    pack_Control_To_PC_Humidity *Humidity_Data;
    pack_Control_To_PC_SASTankStatus *SASTankStatus_Data;
    pack_Control_To_PC_PressureHeight *PressureHeight_Data;
    pack_PC_To_Control_PressureHeight_EN *PressureHeightEN_Data;
    pack_Control_To_PC_AltitudeSensor *AltitudeSensor_Data;
    pack_PC_To_Control_AltitudeSensor_EN *AltitudeSensorEN_Data;
    pack_PC_To_Control_SoundVelocity *SoundVelocity_Data;
    pack_Control_To_PC_Altimeter *Altimeter_Data;

    pack_Control_To_SignalType_H *signaltype_Data_H;
    pack_Control_To_SignalType_L *signaltype_Data_L;
    pack_PC_To_Control_TVG_H *TVG_Data_H;
    pack_PC_To_Control_TVG_L *TVG_Data_L;

    int strHexToDecimal(const QString &strHex);
    char convertHexChar(char ch);
    QByteArray asciiStrToByteArray(const QString &str);
    QByteArray hexStrToByteArray(const QString &str);
};

#endif // SERVER_H
